#!/usr/bin/env python

import re
import sys
import os

num = int(sys.argv[1]) # num of servers
# sys.argv[2]: check which file's causality

def larger_than(x,y):
        result = False
        cnt = 0
        for i in range(len(x)):
                if x[i]>=y[i]:
                        cnt+=1
        if cnt == len(x):
                result = True
        return result	

def diff(file1, file2):
        global flag 
        f1 = open(file1, 'r')
        f2 = open(file2, 'r')
        l1 = f1.readline()
        l2 = f2.readline()
        while l1!= '' and l2!='':
                if l1 != l2:
                        print file1, file2, ' are not the same!'
                        flag = 1
                        break
                l1 = f1.readline()
                l2 = f2.readline()
        if l1 != '':
                print file1, 'is longer than', file2, '.',
                print 'The remaining content are the same.'
                return 
        if l2 != '':
                print file2, 'is longer than', file1, '.',
                print 'The remaining content are the same.'
                return
        print file1, 'and', file2, 'are the same.'
        

flag = 0
for i in range(num):
        for j in range(i+1,num):
                f1 = str(i)+'-fs.log'
                f2 = str(j)+'-fs.log'
                #print 'compare ', f1, f2
                diff(f1, f2)

if flag == 0:
        print '*******************************'
        print 'Relipcated files are the same!'
        print '*******************************'
f = open(sys.argv[2]+'-fs.log')
vectors=[]

for l in f.readlines():
        vc=re.split(':',re.split('\n',l)[0])[1]
        v=re.split(' ', vc)[1:]
        vectors.append(map(int, v))
for i in range(len(vectors)):
        for j in range(i+1,len(vectors)):
                if larger_than(vectors[i],vectors[j]):
                        print vectors[i], 'is larger than', vectors[j], ':Error'
                        sys.exit(1)
print '*******************************'
print 'Causal relation correct!'
print '*******************************'
if flag != 1:                        
        print "Success :)"		
				

